//
//  WOVMovie.m
//  WrapperOfVideo
//
//  Created by James Tuley on 4/10/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "WOVMovie.h"
#import "WOVMovie_Private.h"
#import "WOVMovie_Creator.h"
#import "WOVMovieV1.h"
#import "WOVMovieV2.h"
#import "NonWOVMovie.h"

static NSString* WOVMajorVersionKey= @"MajorVersion";
static NSString* WOVMinorVersionKey= @"MinorVersion";
static NSString* WOVKindKey= @"Kind";
static NSString* WOVRestoreKey= @"Restore";
static NSString* WOVStandardKey= @"Standard";
static NSString* WOVMoovKey= @"Moov";

@implementation WOVMovie
+(bool)writeNewWOVInfoPlist:(WOVMovie*)aWov toPath:(NSString*)aPath{
	

	
	NSMutableArray* tMutArray = [NSMutableArray array];
	NSArray* tArray =[aWov restoreItems];
	NSEnumerator* tEmumerator =[tArray objectEnumerator];
	NSString* tObj;
	
	while(tObj = [tEmumerator nextObject]){
		[tMutArray addObject:[tObj lastPathComponent]];
	}
	
	
	NSDictionary* tDict =[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:2],WOVMajorVersionKey,
																	[NSNumber numberWithInt:0],WOVMinorVersionKey,
																	[aWov kind],WOVKindKey,
																	tMutArray,WOVRestoreKey,
																	[[aWov  standardPath] lastPathComponent], WOVStandardKey,
																	[[aWov quicktimeMoviePath] lastPathComponent], WOVMoovKey,
						  nil];
	return [tDict writeToFile:aPath atomically:YES];
	
}

-(void)dealloc
{
	if(infoDict!=nil)
		[infoDict release];
	if(wovPath !=nil)
		[wovPath release];
	if(extraVars !=nil)
		[extraVars release];
	[super dealloc];
}


+(id)wovWithPath:(NSString*) aPath{
	
	if(![[aPath lowercaseString] hasSuffix:@".wov"] && ![[aPath lowercaseString] hasSuffix:@".wov_inprogress"]){
		return [[NonWOVMovie alloc] initWithDictionary:nil path:aPath];
	}
	
	NSDictionary* tDict = [NSDictionary dictionaryWithContentsOfFile:[aPath stringByAppendingPathComponent:@"Info.plist"]];
	int tVersion =[WOVMovie majorVersion:tDict];
	
	if(tVersion > [WOVMovie maxWOVVersion]){
		[[NSException exceptionWithName:@"NEWER_WOV" reason:@"Unsupported WOV file, too new, update software" userInfo:nil]raise];
	}
	
	
	switch(tVersion){
		case 1:
			return [[[WOVMovieV1 alloc] initWithDictionary:tDict path:aPath] autorelease];
		case 2:
			return [[[WOVMovieV2 alloc] initWithDictionary:tDict path:aPath] autorelease];
		default:
			[[NSException exceptionWithName:@"UNKNOWN_WOV" reason:@"Unknown file version" userInfo:nil]raise];
			return nil;
	}

}


+(int)maxWOVVersion{
	return 2;
}

+(int)majorVersion:(id)aDictionary{
	return [[aDictionary objectForKey:WOVMajorVersionKey] intValue];
}

+(int)minorVersion:(id)aDictionary{
	return [[aDictionary objectForKey:WOVMinorVersionKey] intValue];
}

+(NSString*)kind:(id)aDictionary{
	return [aDictionary objectForKey:WOVKindKey];
}

-(int)majorVersion{
	return [WOVMovie majorVersion:infoDict];
}

-(int)minorVersion{
	return [WOVMovie minorVersion:infoDict];	
}

-(NSString*)kind{
	return [WOVMovie kind:infoDict];	
}

-(NSString*)infoPlistPath{
	return [wovPath stringByAppendingPathComponent:@"Info.plist"];
			
}

-(BOOL)isWOV{
	return YES;
}

@end
